package logic

import (
	"context"

	"go-weixin/app/guitar/model"
	"go-weixin/app/guitar/rpc/internal/svc"
	"go-weixin/app/guitar/rpc/pb/proto/guitar"

	"github.com/zeromicro/go-zero/core/logx"
	"github.com/zeromicro/x/errors"
)

type GuitarImageListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGuitarImageListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GuitarImageListLogic {
	return &GuitarImageListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GuitarImageListLogic) GuitarImageList(in *guitar.GuitarImageListReq) (*guitar.GuitarImageListResp, error) {
	limit := 10
	offset := (in.Page - 1) * 10
	guitarImageList := make([]model.GuitarImageModel, 0)
	err := l.svcCtx.ChenDb.Limit(limit, int(offset)).Find(&guitarImageList)
	if err != nil {
		return nil, errors.New(7000, "guitar.image.list."+err.Error())
	}
	var res []*guitar.GuitarImageBase
	if len(guitarImageList) > 0 {
		for _, g := range guitarImageList {
			res = append(res, &guitar.GuitarImageBase{
				Id:       g.Id,
				GuitarId: g.GuitarId,
				Url:      g.Url,
			})
		}
	}

	return &guitar.GuitarImageListResp{ImageList: res}, nil
}
